#include<vector>
#include<unordered_map>
using namespace std;
class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        if (nums.size() < 1) return 0;
        unordered_map<int, int>map;
        int ans = 0;
        int sum = 0;
        map[0] = 1;
        for (int i = 0; i < nums.size(); ++i) {
            sum += nums[i];
            if (map.find(sum-k) != map.end())
                ans += map[sum - k];
            ++map[sum];
        }
        return ans;
    }
};